package com.ccit.springbootlaw.mapper;

import com.ccit.springbootlaw.pojo.Case;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface CaseMapper {
    // 根据ID查询案例信息
    @Select("SELECT * FROM cases WHERE id = #{id}")
    Case findById(int id);

    // 根据多条件查询案例信息
    @Select("<script>" +
            "SELECT * FROM cases " +
            "WHERE 1=1 " +
            "<if test='caseInfo.title != null and caseInfo.title != \"\"'>AND title LIKE CONCAT('%', #{caseInfo.title}, '%')</if> " +
            "<if test='caseInfo.content != null and caseInfo.content != \"\"'>AND content = #{caseInfo.content}</if> " +
            "<if test='caseInfo.category != null and caseInfo.category != \"\"'>AND category = #{caseInfo.category}</if> " +
            "<if test='caseInfo.author != null and caseInfo.author != \"\"'>AND author = #{caseInfo.author}</if> " +
            "<if test='startTime != null'>AND casetime &gt;= #{startTime}</if> " +
            "<if test='endTime != null'>AND casetime &lt;= #{endTime}</if> " +
            "</script>")
    List<Case> findByCriteria(@Param("caseInfo") Case caseInfo, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    // 插入新的案例信息
    @Insert("INSERT INTO cases (title, content, category, author, casetime, created_at, updated_at) " +
            "VALUES (#{title}, #{content}, #{category}, #{author}, #{casetime}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertCase(Case caseInfo);

    // 更新案例信息
    @Update("<script>" +
            "UPDATE cases " +
            "<set>" +
            "<if test='title != null and title != \"\"'>title = #{title},</if>" +
            "<if test='content != null and content != \"\"'>content = #{content},</if>" +
            "<if test='category != null and category != \"\"'>category = #{category},</if>" +
            "<if test='author != null and author != \"\"'>author = #{author},</if>" +
            "<if test='casetime != null'>casetime = #{casetime},</if>" +
            "updated_at = NOW()" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int updateCase(Case caseInfo);

    // 删除案例信息
    @Delete("DELETE FROM cases WHERE id = #{id}")
    int deleteCaseById(int id);

    // 查询所有案例信息
    @Select("SELECT * FROM cases")
    List<Case> findAllCases();


}